package com.example.myapplication.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.App;
import com.example.myapplication.R;
import com.example.myapplication.adpter.HomeMainNewsTypeAdapter;
import com.example.myapplication.adpter.HomeMainRecommendAdapter;
import com.example.myapplication.adpter.HomeMainSpecialAdapter;
import com.example.myapplication.base.BaseFragment;
import com.example.myapplication.base.Constant;
import com.example.myapplication.bean.MainHomeBannerBean;
import com.example.myapplication.bean.MainHomeNewsTypeBean;
import com.example.myapplication.bean.MainHomeRecommendBean;
import com.example.myapplication.bean.MainHomeSpecialBean;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;

public class HomeFragment extends BaseFragment {

    private Banner mainBanner = null;
    private RecyclerView recommendRecyclerView = null;
    private RecyclerView specialRecyclerView = null;
    private RecyclerView newsTypeRecyclerView = null;

    @Override
    public View initView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home, null);
        mainBanner = view.findViewById(R.id.mainBanner);
        recommendRecyclerView = view.findViewById(R.id.recommendRecyclerView);
        specialRecyclerView = view.findViewById(R.id.specialRecyclerView);
        newsTypeRecyclerView = view.findViewById(R.id.newsTypeRecyclerView);
        return view;
    }

    @Override
    public void initData() {

        getBanner();//取首页轮播广告
        getRecommendService();//取推荐服务
        getSpecial();//获取专题列表
        getNewsType();//获取新闻类型

    }

    @Override
    public void initEvent() {

    }

    private void getBanner() {
        RequestQueue queue = Volley.newRequestQueue(App.get());
        String url = Constant.API_URL + "rotation/list?type=2";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, response -> {
            //这里是请求成功执行的代码块 response 为字符串对象
            MainHomeBannerBean bean = new Gson().fromJson(response, MainHomeBannerBean.class);
            mainBanner.setAdapter(new BannerImageAdapter<MainHomeBannerBean.RowsDTO>(bean.getRows()) {
                @Override
                public void onBindView(BannerImageHolder holder, MainHomeBannerBean.RowsDTO data, int position, int size) {
                    RequestOptions requestOptions = new RequestOptions().transform(new CenterCrop(), new RoundedCorners(16));
                    Glide.with(holder.itemView).load(Constant.URL + data.getAdvImg()).apply(requestOptions).into(holder.imageView);
                }
            });
        }, error -> {
            //这里是请求失败的代码块

        });
        queue.add(stringRequest);
    }

    private void getRecommendService() {

        RequestQueue queue = Volley.newRequestQueue(App.get());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Constant.API_URL + "service/list", response -> {
            try {
                response = new String(response.getBytes("ISO-8859-1"), "utf-8");
                MainHomeRecommendBean bean = new Gson().fromJson(response, MainHomeRecommendBean.class);
                HomeMainRecommendAdapter adapter = new HomeMainRecommendAdapter(bean.getRows());
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(App.get());
                linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
                recommendRecyclerView.setLayoutManager(linearLayoutManager);
                recommendRecyclerView.setAdapter(adapter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, error -> {

        });
        queue.add(stringRequest);

    }

    private void getSpecial() {

        RequestQueue queue = Volley.newRequestQueue(App.get());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Constant.PRESS_URL + "press/list?hot=Y", response -> {
            try {
                response = new String(response.getBytes("ISO-8859-1"), "utf-8");
                MainHomeSpecialBean bean = new Gson().fromJson(response, MainHomeSpecialBean.class);
                HomeMainSpecialAdapter adapter = new HomeMainSpecialAdapter(bean.getRows());
                GridLayoutManager gridLayoutManager = new GridLayoutManager(App.get(),2);
                specialRecyclerView.setLayoutManager(gridLayoutManager);
                specialRecyclerView.setAdapter(adapter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, error -> {

        });
        queue.add(stringRequest);

    }

    private void getNewsType() {

        RequestQueue queue = Volley.newRequestQueue(App.get());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Constant.PRESS_URL + "category/list", response -> {
            try {
                response = new String(response.getBytes("ISO-8859-1"), "utf-8");
                MainHomeNewsTypeBean bean = new Gson().fromJson(response, MainHomeNewsTypeBean.class);
                HomeMainNewsTypeAdapter adapter = new HomeMainNewsTypeAdapter(bean.getData());
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(App.get());
                linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
                newsTypeRecyclerView.setLayoutManager(linearLayoutManager);
                newsTypeRecyclerView.setAdapter(adapter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, error -> {

        });
        queue.add(stringRequest);

    }

}
